#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash gkl-0.8.10.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar environment-modules git cmake automake patch gcc-c++ libtool libatomic zlib-devel libstdc++-static

if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/Intel-HLS/GKL/archive/refs/tags/0.8.10.tar.gz`
then 
   echo "0.8.10.tar.gz下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/Intel-HLS/GKL/archive/refs/tags/0.8.10.tar.gz`
then 
   echo "0.8.10.tar.gz下载成功!"
else
   echo "0.8.10.tar.gz下载失败!"
fi

if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/kunpengcompute/AvxToNeon/archive/refs/tags/v1.0.0.tar.gz`
then 
   echo "v1.0.0.tar.gz下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/kunpengcompute/AvxToNeon/archive/refs/tags/v1.0.0.tar.gz`
then 
   echo "v1.0.0.tar.gz下载成功!"
else
   echo "v1.0.0.tar.gz下载失败!"
fi

tar xf $buildpath/0.8.10.tar.gz -C $buildpath && rm -rf $buildpath/0.8.10.tar.gz
tar xf $buildpath/v1.0.0.tar.gz -C $buildpath && rm -rf $buildpath/v1.0.0.tar.gz

src_path=$(cd $(dirname "${BASH_SOURCE[0]}") >/dev/null && pwd)
cp $src_path/src/avx_extra.h $buildpath/GKL-0.8.10/src/main/native/pairhmm
cp $src_path/src/smith_avxextra.h $buildpath/GKL-0.8.10/src/main/native/smithwaterman

cd "$buildpath"

sed -i "7ainclude_directories($buildpath/AvxToNeon-1.0.0)" GKL-0.8.10/CMakeLists.txt

sed -i "s/#include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "s/#include <cpuid.h>/\/\/&/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "32a\    #include \"avx2neon.h\"" GKL-0.8.10/src/main/native/common/avx.h
sed -i "42c\/*\    uint32_t xcr0;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "48a*\/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "49a\    return false;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "57c\/\/    uint32_t xcr0;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "58c\/\/    uint32_t zmm_ymm_xmm = (7 << 5) | (1 << 2) | (1 << 1);" GKL-0.8.10/src/main/native/common/avx.h
sed -i "59c\/\/#if defined(_MSC_VER)" GKL-0.8.10/src/main/native/common/avx.h
sed -i "61c\/\/    xcr0 = (uint32_t)_xgetbv(0);" GKL-0.8.10/src/main/native/common/avx.h
sed -i "62c\/\/#else" GKL-0.8.10/src/main/native/common/avx.h
sed -i "63c\/\/    __asm__ (\"xgetbv\" : \"=a\" (xcr0) : \"c\" (0) : \"%edx\");" GKL-0.8.10/src/main/native/common/avx.h
sed -i "64c\/\/#endif" GKL-0.8.10/src/main/native/common/avx.h
sed -i "66c\/\/    return ((xcr0 & zmm_ymm_xmm) == zmm_ymm_xmm);" GKL-0.8.10/src/main/native/common/avx.h
sed -i "66a\    return false;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "76c\/*\    uint32_t a, b, c, d;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "90a*\/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "91a\    return true;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "101c\/*\    uint32_t a, b, c, d;" GKL-0.8.10/src/main/native/common/avx.h
sed -i "126c*\/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "135a\/*" GKL-0.8.10/src/main/native/common/avx.h
sed -i "168a*\/" GKL-0.8.10/src/main/native/common/avx.h
sed -i "169a\    return true;" GKL-0.8.10/src/main/native/common/avx.h

sed -i "s/# include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/encode_df.c
sed -i "11a# include \"avx2neon.h\"" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/encode_df.c

sed -i "s/# include <immintrin.h>/\/\/&/" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huff_codes.h
sed -i "40a# include \"avx2neon.h\"" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huff_codes.h
sed -i "s/# include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huff_codes.h
sed -i "45a# include \"avx2neon.h\"" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huff_codes.h

sed -i "s/# include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huffman.h
sed -i "41a# include \"avx2neon.h\"" GKL-0.8.10/src/main/native/compression/isa-l-master/igzip/huffman.h

sed -i "26a# include \"avx_extra.h\"" GKL-0.8.10/src/main/native/pairhmm/avx-types.h

sed -i "s/set_property/#&/g" GKL-0.8.10/src/main/native/pairhmm/CMakeLists.txt

sed -i "93c\  \/\/if (_MM_GET_FLUSH_ZERO_MODE() != _MM_FLUSH_ZERO_ON) {" GKL-0.8.10/src/main/native/pairhmm/IntelPairHmm.cc
sed -i "94c\  \/\/  DBG(\"Flush-to-zero (FTZ) is enabled when running PairHMM\");" GKL-0.8.10/src/main/native/pairhmm/IntelPairHmm.cc
sed -i "95c\  \/\/}" GKL-0.8.10/src/main/native/pairhmm/IntelPairHmm.cc
sed -i "96c\  \/\/_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);" GKL-0.8.10/src/main/native/pairhmm/IntelPairHmm.cc

sed -i "s/#include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/pairhmm/pairhmm_common.h
sed -i "30a\  #include \"avx2neon.h\"" GKL-0.8.10/src/main/native/pairhmm/pairhmm_common.h

sed -i "45c#include \"smith_avxextra.h\"" GKL-0.8.10/src/main/native/smithwaterman/avx2-functions.h
sed -i "98c\    (__m256i)rev_interpret(_mm256_blendv_ps(interpret(__v1), (__m256)interpret(__v2), (__m256)interpret(__mask)))" GKL-0.8.10/src/main/native/smithwaterman/avx2-functions.h

sed -i "s/set_property/#&/g" GKL-0.8.10/src/main/native/smithwaterman/CMakeLists.txt
sed -i "7aset_property(SOURCE avx2_impl.cc APPEND_STRING PROPERTY COMPILE_FLAGS \" -march=armv8-a+fp+simd+crc \")" GKL-0.8.10/src/main/native/smithwaterman/CMakeLists.txt
sed -i "9aset_property(SOURCE avx512_impl.cc APPEND_STRING PROPERTY COMPILE_FLAGS \" -march=armv8-a+fp+simd+crc \")" GKL-0.8.10/src/main/native/smithwaterman/CMakeLists.txt

sed -i "s/#include <immintrin.h>/\/\/&/" GKL-0.8.10/src/main/native/smithwaterman/IntelSmithWaterman.cc
sed -i "34a#include \"avx2neon.h\"" GKL-0.8.10/src/main/native/smithwaterman/IntelSmithWaterman.cc

sed -i "25a#include \"smith_avxextra.h\"" GKL-0.8.10/src/main/native/smithwaterman/PairWiseSW.h

sed -i "s/#include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/smithwaterman/smithwaterman_common.h
sed -i "30a\  #include \"avx2neon.h\"" GKL-0.8.10/src/main/native/smithwaterman/smithwaterman_common.h
sed -i "s/#include <immintrin.h>/\/\/&/" GKL-0.8.10/src/main/native/smithwaterman/smithwaterman_common.h
sed -i "38a#include \"avx2neon.h\"" GKL-0.8.10/src/main/native/smithwaterman/smithwaterman_common.h

sed -i "s/#include <x86intrin.h>/\/\/&/" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "27a\  #include \"avx2neon.h\"" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "46s/value = /&0;\/\//" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "58,65s/^/\/\//" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "76s/^/\/\//" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "76a\    return 1;" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "88s/^/\/\//" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "88a\    return 1;" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "100s/^/\/\//" GKL-0.8.10/src/main/native/utils/utils.cc
sed -i "100a\    return 1;" GKL-0.8.10/src/main/native/utils/utils.cc

sed -i "120s/^/\/\//" GKL-0.8.10/build.gradle

cd GKL-0.8.10
gkl_build=`./gradlew test`

mkdir -p "$installpath"/gkl-0.8.10
cp -a ./* "$installpath"/gkl-0.8.10

rm -rf "$buildpath"/GKL-0.8.10
rm -rf "$buildpath"/AvxToNeon-1.0.0